﻿using RuoVea.ExCache.Cache;
using RuoVea.ExCache.Enum;
using RuoVea.ExConfig;

namespace RuoVea.ExCache
{
    /// <summary>
    /// 创建人：starry
    /// 描 述：定义缓存 - 缓存配置
    /// </summary>
    public class CacheFactery
    {
        /// <summary>
        /// 缓存类型
        /// </summary>
        public static CacheEnum _cacheType { get; set; } = (AppSettings.GetValue(new string[] { "Cache", "CacheType" })?.ToString().ToLower() == "RedisCache".ToLower()) ? CacheEnum.RedisCache : CacheEnum.MemoryCache;
        /// <summary>
        /// Redis配置
        /// </summary>
        internal static string _redisConnectionString { get; set; } = AppSettings.GetValue(new string[] { "Cache", "RedisConnectionString" })?.ToString();
        /// <summary>
        /// 缓存前缀
        /// </summary>
        internal static string _prefix { get; set; } = AppSettings.GetValue(new string[] { "Cache", "Prefix" })?.ToString();

        /// <summary>
        /// Cache
        /// </summary>
        /// <returns>ICache</returns>
        public static ICache CaChe
        {
            get
            {
                if (_cacheType == CacheEnum.RedisCache)
                {
                    return new RedisCache(_prefix, _redisConnectionString);
                }
                else
                {   //普通缓存
                    return new MemoryCaches(_prefix);
                }
            }
        }
    }
}
